package com.gdth.bank.model;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppBfrjJbxxb;
import com.gdth.database.bean.TAppBfrjJbxxbDao;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjJtcybDao;
import com.gdth.database.bean.TAppBfrjLxfs;
import com.gdth.database.bean.TAppBfrjLxfsDao;
import com.gdth.http.method.HttpMethodServlet;

import org.greenrobot.greendao.query.QueryBuilder;


/**
 * 采访客户基本信息
 * @author arvin
 *
 */
public class CJkhxxModel {
	/**
	 * 采访客户基本信息保存
	 * @param mTAppBfrjJbxxb
	 * @return
	 */
	public Observable<TAppBfrjJbxxb> saveCjkhjbxx(final TAppBfrjJbxxb mTAppBfrjJbxxb){
		return Observable.create(new Observable.OnSubscribe<TAppBfrjJbxxb>() {

			@Override
			public void call(final Subscriber<? super TAppBfrjJbxxb> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjJbxxbDao dao = session.getTAppBfrjJbxxbDao();
						
						QueryBuilder<TAppBfrjJbxxb> khxxqb = dao.queryBuilder();
						khxxqb.where(TAppBfrjJbxxbDao.Properties.Zjhm.eq(mTAppBfrjJbxxb.getZjhm()),
								TAppBfrjJbxxbDao.Properties.Cjrsfz.eq(mTAppBfrjJbxxb.getCjrsfz()),
								TAppBfrjJbxxbDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete));
						long count = khxxqb.count();
						if (count > 0) {
							ex = new Exception("该客户已存在");
							arg0.onError(ex);
							
							return;
						}
						
						try {
							dao.insert(mTAppBfrjJbxxb);
							if (mTAppBfrjJbxxb.getJtcys() != null) {
								TAppBfrjJtcybDao jtcyDao = session.getTAppBfrjJtcybDao();
								TAppBfrjJtcyb mTAppBfrjJtcyb = mTAppBfrjJbxxb.getJtcys().get(0);
								jtcyDao.insert(mTAppBfrjJtcyb);
							}
							if (mTAppBfrjJbxxb.getLxfss() != null) {
								TAppBfrjLxfsDao lxfsDao = session.getTAppBfrjLxfsDao();
								TAppBfrjLxfs mTAppBfrjLxfs = mTAppBfrjJbxxb.getLxfss().get(0);
								lxfsDao.insert(mTAppBfrjLxfs);
							}
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							arg0.onNext(mTAppBfrjJbxxb);
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}

	/**
	 * 采访客户基本信息保存
	 * @param mTAppBfrjJbxxb
	 * @return
	 */
	public Observable<TAppBfrjJbxxb> updataCjkhjbxx(final TAppBfrjJbxxb mTAppBfrjJbxxb){
		return Observable.create(new Observable.OnSubscribe<TAppBfrjJbxxb>() {

			@Override
			public void call(final Subscriber<? super TAppBfrjJbxxb> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjJbxxbDao dao = session.getTAppBfrjJbxxbDao();

						QueryBuilder<TAppBfrjJbxxb> khxxqb = dao.queryBuilder();
						khxxqb.where(TAppBfrjJbxxbDao.Properties.Uuid.eq(mTAppBfrjJbxxb.getUuid()),
								TAppBfrjJbxxbDao.Properties.Cjrsfz.eq(mTAppBfrjJbxxb.getCjrsfz()),
								TAppBfrjJbxxbDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete));
						long count = khxxqb.count();
						if (count > 0) {
							List<TAppBfrjJbxxb> mlist = khxxqb.list();
							for (TAppBfrjJbxxb tAppBfrjJbxxb : mlist) {
								tAppBfrjJbxxb.setKhxm(mTAppBfrjJbxxb.getKhxm());
								tAppBfrjJbxxb.setZjhm(mTAppBfrjJbxxb.getZjhm());
								tAppBfrjJbxxb.setSjzt(AppUtil.sjzt_update);
								dao.update(tAppBfrjJbxxb);
							}
							arg0.onNext(mTAppBfrjJbxxb);
							arg0.onCompleted();
							return;
						}else{
							ex = new Exception("该客户无法修改");
							arg0.onError(ex);
							return;
						}
					}
				});
			}
		});
	}

	
	/**
	 * 更新状态
	 * @param jcuuid
	 */
	public static void updateCjkhJbxxZt(String jcuuid) {
		DaoSession session = BankApplication.getDaoSession();
		
		TAppBfrjJbxxbDao dao = session.getTAppBfrjJbxxbDao();
		
		QueryBuilder<TAppBfrjJbxxb> khxxqb = dao.queryBuilder();
		khxxqb.where(TAppBfrjJbxxbDao.Properties.Uuid.eq(jcuuid));
		List<TAppBfrjJbxxb> mList = khxxqb.list();
		for (TAppBfrjJbxxb mTAppBfrjJbxxb : mList) {
			mTAppBfrjJbxxb.setSjzt(AppUtil.sjzt_update);
			dao.update(mTAppBfrjJbxxb);
		}
	}
}
